/**
 * 2010-5-13
 * InnovationServer
 * @author lyh
 */
package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.config.Config;
import labox.innovation.gameserver.instancemanager.QuestManager;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.quest.Quest;
import labox.innovation.gameserver.model.quest.QuestState;
import labox.innovation.gameserver.model.quest.QuestStateLogic;
import labox.innovation.gameserver.network.serverpackets.ClientScriptHandle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestQuestAbort extends L2GameClientPacket {
	private static final String _C__53_REQUESTQUESTABORT = "[C] 53 RequestQuestAbort";

	private static final Logger _log = LoggerFactory.getLogger(RequestQuestAbort.class.getName());

	private String _command;

	@Override
	public String getType() {
		return _C__53_REQUESTQUESTABORT;
	}

	@Override
	protected void readImpl() {
		String str = readString();
		_command = str.replaceAll("\"", "");
	}

	@Override
	protected void runImpl() {
		FuzePcInstance activeChar = getClient().getActiveChar();

		if (activeChar == null)
			return;
		try {
			if (_command.length()>0) {
				// 放弃任务
				String qn = _command.trim();
				Quest qe = QuestManager.getInstance().getQuest(qn);
				if (qe != null) {
					QuestState qs = activeChar.getQuestState(qn);
					if (qs != null) {
						qs.exitQuest(true);
						String scriptStr = "remove(\"" + qn + "\")";
						sendPacket(new ClientScriptHandle("yj", scriptStr));
						QuestStateLogic.getInstance().updateNpcFace(activeChar);
					} else {
						_log.info("Player '" + activeChar.getName() + "' try to quit quest " + qe.getName() + " but he didn't have it started.", Config.SERVER_ID, activeChar.getName());
					}
				} else {
					_log.warn("Quest (name='{" + qn + "}') not found.", Config.SERVER_ID, "取消任务");
				}

			}
		} catch (Exception e) {
			_log.error( "{" + getClient() + "} sent bad RequestQuestAbort: ", Config.SERVER_ID, e);
		}
	}

}
